package Canvas;
import javax.swing.*;
import java.awt.*;

class MyCanvas extends Canvas
{
    public void paint(Graphics g) // 自动传
    {
        g.setColor(Color.RED);
        g.drawString("这是一个Canvas示例",10,10);
        g.setColor(Color.GREEN);
        g.drawRect(20,25,100,20);
        g.setColor(Color.BLUE);
        g.drawOval(20,65,60,60);
    }
}

public class MyFrame extends JFrame{
    public MyFrame()
    {
        this.setSize(350,200);
        this.setTitle("Canvas Demo.");
        this.setLayout(null); // 将JFrame的布局设置为空，通过坐标定位

        MyCanvas myc = new MyCanvas();
        myc.setBounds(10,20,260,200);
        this.add(myc);

        this.setLocationRelativeTo(null); // 使窗口居中
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 响应窗口关闭按钮
        this.setVisible(true); // 将窗口显示出来
    }
    public static void main(String[] args)
    {
        new MyFrame();
    }
}